package com.dmgameclient.common;

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.LinkedList;

import javax.crypto.spec.PSource;

import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.DefaultHttpClient;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.params.HttpConnectionParams;
import org.apache.http.params.HttpParams;
import org.apache.http.util.EntityUtils;

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Path;
import android.util.Log;

/**
 * @name 网络工具类
 * @Descripation <br>
 *               1、从指定路径下载字符串。<br>
 *               2、从指定路径下载一张图片byte[]。<br>
 *               3、从指定路径下载一张图片bitmap。
 * @author 樊俊彬
 * @date 2014-9-9
 * @version 1.0
 */
public class HttpUtils {

	private static final String TAG = "HttpUtils";

	/**
	 * 从网络下载字符串
	 * 
	 * @param path
	 * @param encode
	 * @return String
	 */
	public static String downloadString(String path, String encode) {

		if (path == null || encode == null) {
			return null;
		}
		String url = path.trim();
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);

		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 60000);
		HttpConnectionParams.setSoTimeout(httpParams, 60000);

		HttpResponse response = null;
		try {
			response = client.execute(get);
			String result = EntityUtils.toString(response.getEntity(), encode);
			if (response.getStatusLine().getStatusCode() == 200) {
				Log.i(TAG, ">>>downstring completed length=" + result.length());
				return result;
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {

			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return null;
	}

	/**
	 * 上传一个字符串到指定URL路径
	 * 
	 * @param path
	 * @param aid
	 * @param msg
	 */
	public static String uploadString(String path, String encode, String aid,
			String msg) {
		if (path == null) {
			return null;
		}
		String url = path.trim();
		HttpClient client = new DefaultHttpClient();
		HttpResponse response;
		HttpPost post = new HttpPost(url);

		// 请求参数列表
		LinkedList<NameValuePair> params = new LinkedList<NameValuePair>();
		params.add(new BasicNameValuePair("aid", aid));
		params.add(new BasicNameValuePair("msg", msg));

		try {
			UrlEncodedFormEntity entity = new UrlEncodedFormEntity(params);
			post.setEntity(entity);
			// 发送请求
			response = client.execute(post);
			if (response.getStatusLine().getStatusCode() == 200) {
				return EntityUtils.toString(response.getEntity(), encode);
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}
		return null;
	}

	/**
	 * 从网络下载图片
	 * 
	 * @param path
	 * @return byte[]
	 */
	public static byte[] downloadImage(String path) {
		if (path == null || path.equals("")) {
			return null;
		}

		String url = path.trim();
		HttpClient client = new DefaultHttpClient();
		HttpGet get = new HttpGet(url);

		HttpParams httpParams = client.getParams();
		HttpConnectionParams.setConnectionTimeout(httpParams, 60000);
		HttpConnectionParams.setSoTimeout(httpParams, 60000);

		HttpResponse response = null;
		HttpEntity entity = null;

		InputStream inputStream = null;
		ByteArrayOutputStream byteArrayOutputStream = new ByteArrayOutputStream();

		try {
			response = client.execute(get);
			entity = response.getEntity();
			if (response.getStatusLine().getStatusCode() == 200) {
				inputStream = entity.getContent();
				int len = 0;
				byte[] buffer = new byte[2048];
				while ((len = inputStream.read(buffer)) != -1) {
					byteArrayOutputStream.write(buffer, 0, len);
				}
				Log.i(TAG,
						">>>图片下载成功>>" + url + ",大小"
								+ byteArrayOutputStream.toByteArray().length
								/ 1024 + "KB");
				return byteArrayOutputStream.toByteArray();
			}

		} catch (ClientProtocolException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} finally {
			if (inputStream != null) {
				try {
					inputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (byteArrayOutputStream != null) {
				try {
					byteArrayOutputStream.close();
				} catch (IOException e) {
					// TODO Auto-generated catch block
					e.printStackTrace();
				}
			}

			if (client != null) {
				client.getConnectionManager().shutdown();
			}
		}

		return null;
	}

	/**
	 * 从网络下载Bitmap
	 * 
	 * @param path
	 * @return Bitmap
	 */
	public static Bitmap downloadBitmap(String path) {
		byte[] data = downloadImage(path);
		if (data != null) {
			return BitmapFactory.decodeByteArray(data, 0, data.length);
		}
		return null;
	}
}
